package com.imooc.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * FileName: RedisUtils.java
 * Description: TODO
 *
 * @author HuangRI(874668739 @ qq.com)
 * @version 1.0
 * @date 2021/1/4
 */
@Component
public class RedisUtil {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 设置string类型的key
     * @param key
     * @param value
     */
    public void set(String key, Object value) {
        this.redisTemplate.opsForValue().set(key, value);
    }

    /**
     * 设置key-value，并设置过期时间，单位秒
     * @param key
     * @param value
     * @param timeout
     */
    public void set(String key, Object value, long timeout) {
        this.redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
    }

    /**
     * 获取key的value
     * @param key
     * @return
     */
    public Object get(Object key) {
        return this.redisTemplate.opsForValue().get(key);
    }

    /**
     * 设置过期时间，单位秒
     * @param key
     * @param timeout
     */
    public void expire(Object key, long timeout) {
        this.redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
    }

    /**
     * 删除key
     * @param key
     */
    public void delete(Object key) {
        this.redisTemplate.delete(key);
    }
}
